home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / RKMLibsPrgs / intuition / mouse_keyboard / mousetest.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  9KB  |  274 lines

  1. ;/* mousetest.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 mousetest.c
  3. Blink FROM LIB:c.o,mousetest.o TO mousetest LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7. /*
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33. /*
  34. ** mousetest.c - Read position and button events from the mouse.
  35. */
  36. #define INTUI_V36_NAMES_ONLY
  37.  
  38. #include <exec/types.h>
  39. #include <intuition/intuition.h>
  40. #include <graphics/gfxbase.h>
  41. #include <devices/inputevent.h>
  42.  
  43. #include <clib/exec_protos.h>
  44. #include <clib/graphics_protos.h>
  45. #include <clib/intuition_protos.h>
  46.  
  47. #include <stdio.h>
  48.  
  49. #ifdef LATTICE
  50. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  51. int chkabort(void) { return(0); }  /* really */
  52. #endif
  53.  
  54. #define BUFSIZE 16
  55.  
  56. /* something to use to track the time between messages
  57. ** to test for double-clicks.
  58. */
  59. typedef struct myTimeVal
  60.     {
  61.     ULONG LeftSeconds;
  62.     ULONG LeftMicros;
  63.     ULONG RightSeconds;
  64.     ULONG RightMicros;
  65.     } MYTIMEVAL;
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74. /* our function prototypes */
  75. VOID doButtons(struct IntuiMessage *msg, MYTIMEVAL *tv);
  76. VOID process_window(struct Window *win);
  77.  
  78. struct Library *IntuitionBase;
  79. struct GfxBase       *GfxBase;  /* we need GfxBase->DefaultFont */
  80.  
  81.  
  82. /*
  83. ** main() -- set-up everything.
  84. */
  85. VOID main(int argc, char **argv)
  86. {
  87. struct Window *win;
  88. struct Screen *scr;
  89. struct DrawInfo *dr_info;
  90. ULONG width;
  91.  
  92. /* Open the libraries we will use.  Requires Release 2 (KS V2.04, V37) */
  93. if (IntuitionBase = OpenLibrary("intuition.library",37))
  94.     {
  95.     if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 37))
  96.         {
  97.         /* Lock the default public screen in order to read its DrawInfo data */
  98.         if (scr = LockPubScreen(NULL))
  99.             {
  100.             if (dr_info = GetScreenDrawInfo(scr))
  101.                 {
  102.                 /* use wider of space needed for output (18 chars and spaces)
  103.                  * or titlebar text plus room for titlebar gads (approx 18 each)
  104.                  */
  105.                 width = max((GfxBase->DefaultFont->tf_XSize * 18),
  106.                             (18 * 2) + TextLength(&scr->RastPort,"MouseTest",9));
  107.  
  108.                 if (win = OpenWindowTags(NULL,
  109.                             WA_Top,    20,
  110.                             WA_Left,   100,
  111.                             WA_InnerWidth,  width,
  112.                             WA_Height, (2 * GfxBase->DefaultFont->tf_YSize) +
  113.                                        scr->WBorTop + scr->Font->ta_YSize + 1 +
  114.                                        scr->WBorBottom,
  115.                             WA_Flags, WFLG_DEPTHGADGET | WFLG_CLOSEGADGET |
  116.                                       WFLG_ACTIVATE    | WFLG_REPORTMOUSE |
  117.                                       WFLG_RMBTRAP     | WFLG_DRAGBAR,
  118.                             WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_RAWKEY |
  119.                                       IDCMP_MOUSEMOVE   | IDCMP_MOUSEBUTTONS,
  120.                             WA_Title, "MouseTest",
  121.                             WA_PubScreen, scr,
  122.                             TAG_END))
  123.                     {
  124.                     printf("Monitors the Mouse:\n");
  125.                     printf("    Move Mouse, Click and DoubleClick in Window\n");
  126.  
  127.                     SetAPen(win->RPort,dr_info->dri_Pens[TEXTPEN]);
  128.                     SetBPen(win->RPort,dr_info->dri_Pens[BACKGROUNDPEN]);
  129.                     SetDrMd(win->RPort,JAM2);
  130.  
  131.                     process_window(win);
  132.  
  133.                     CloseWindow(win);
  134.                     }
  135.                 FreeScreenDrawInfo(scr, dr_info);
  136.                 }
  137.             UnlockPubScreen(NULL,scr);
  138.             }
  139.         CloseLibrary((struct Library *)GfxBase);
  140.         }
  141.     CloseLibrary(IntuitionBase);
  142.     }
  143. }
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. /*
  151. ** process_window() - simple message loop for processing IntuiMessages
  152. */
  153. VOID process_window(struct Window *win)
  154. {
  155. USHORT done;
  156. struct IntuiMessage *msg;
  157. MYTIMEVAL tv;
  158. UBYTE prt_buff[14];
  159. LONG xText, yText;  /* places to position text in window. */
  160.  
  161. done = FALSE;
  162. tv.LeftSeconds = 0; /* initial values for testing double-click */
  163. tv.LeftMicros  = 0;
  164. tv.RightSeconds = 0;
  165. tv.RightMicros  = 0;
  166. xText = win->BorderLeft + (win->IFont->tf_XSize * 2);
  167. yText = win->BorderTop + 3 + win->IFont->tf_Baseline;
  168.  
  169. while (!done)
  170.     {
  171.     Wait((1L<<win->UserPort->mp_SigBit));
  172.  
  173.     while ((!done) &&
  174.            (msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
  175.         {
  176.         switch (msg->Class)
  177.             {
  178.             case IDCMP_CLOSEWINDOW:
  179.                 done = TRUE;
  180.                 break;
  181.             /* NOTE NOTE NOTE:  If the mouse queue backs up a lot, Intuition
  182.             ** will start dropping MOUSEMOVE messages off the end until the
  183.             ** queue is serviced.  This may cause the program to lose some
  184.             ** of the MOUSEMOVE events at the end of the stream.
  185.             **
  186.             ** Look in the window structure if you need the true position
  187.             ** of the mouse pointer at any given time.  Look in the
  188.             ** MOUSEBUTTONS message if you need position when it clicked.
  189.             ** An alternate to this processing would be to set a flag that
  190.             ** a mousemove event arrived, then print the position of the
  191.             ** mouse outside of the "while (GetMsg())" loop.  This allows
  192.             ** a single processing call for many mouse events, which speeds
  193.             ** up processing A LOT!  Something like:
  194.             **
  195.             ** while (GetMsg())
  196.             **    {
  197.             **    if (class == IDCMP_MOUSEMOVE)
  198.             **        mouse_flag = TRUE;
  199.             **    ReplyMsg();   NOTE: copy out all needed fields first !
  200.             **    }
  201.             ** if (mouse_flag)
  202.             **    {
  203.             **    process_mouse_event();
  204.             **    mouse_flag = FALSE;
  205.             **    }
  206.             **
  207.             ** You can also use IDCMP_INTUITICKS for slower paced messages
  208.             ** (all messages have mouse coordinates.)
  209.             */
  210.             case IDCMP_MOUSEMOVE:
  211.                 /* Show the current position of the mouse relative to the
  212.                 ** upper left hand corner of our window
  213.                 */
  214.                 Move(win->RPort,xText,yText);
  215.                 sprintf(prt_buff, "X%5d Y%5d", msg->MouseX, msg->MouseY);
  216.                 Text(win->RPort,prt_buff,13);
  217.                 break;
  218.             case IDCMP_MOUSEBUTTONS:
  219.                 doButtons(msg,&tv);
  220.                 break;
  221.             }
  222.         ReplyMsg((struct Message *)msg);
  223.         }
  224.     }
  225. }
  226.  
  227. /*
  228. ** Show what mouse buttons where pushed
  229. */
  230. VOID doButtons(struct IntuiMessage *msg, MYTIMEVAL *tv)
  231. {
  232. /* Yes, qualifiers can apply to the mouse also.  That is how
  233. ** we get the shift select on the Workbench.  This shows how
  234. ** to see if a specific bit is set within the qualifier
  235. */
  236. if (msg->Qualifier & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT))
  237.     printf("Shift ");
  238.  
  239. switch (msg->Code)
  240.     {
  241.     case SELECTDOWN:
  242.         printf("Left Button Down at X%ld Y%ld", msg->MouseX, msg->MouseY);
  243.         if(DoubleClick(tv->LeftSeconds, tv->LeftMicros, msg->Seconds, msg->Micros))
  244.             printf(" DoubleClick!");
  245.         else
  246.             {
  247.             tv->LeftSeconds = msg->Seconds;
  248.             tv->LeftMicros  = msg->Micros;
  249.             tv->RightSeconds = 0;
  250.             tv->RightMicros  = 0;
  251.             }
  252.         break;
  253.     case SELECTUP:
  254.         printf("Left Button Up   at X%ld Y%ld", msg->MouseX, msg->MouseY);
  255.         break;
  256.     case MENUDOWN:
  257.         printf("Right Button down at X%ld Y%ld", msg->MouseX, msg->MouseY);
  258.         if(DoubleClick(tv->RightSeconds, tv->RightMicros, msg->Seconds, msg->Micros))
  259.             printf(" DoubleClick!");
  260.         else
  261.             {
  262.             tv->LeftSeconds = 0;
  263.             tv->LeftMicros  = 0;
  264.             tv->RightSeconds = msg->Seconds;
  265.             tv->RightMicros  = msg->Micros;
  266.             }
  267.         break;
  268.     case MENUUP:
  269.         printf("Right Button Up   at X%ld Y%ld", msg->MouseX, msg->MouseY);
  270.         break;
  271.     }
  272. printf("\n");
  273. }
  274.